#include "ImageControl.h"

CImageControl* CImageControl::NewL(const TRect& aRect,const CCoeControl* aParent)
	{
	CImageControl* self = CImageControl::NewLC(aRect,aParent);
    CleanupStack::Pop(self);
    return self;	
	}

CImageControl* CImageControl::NewLC(const TRect& aRect,const CCoeControl* aParent)
	{
	CImageControl* self = new(ELeave) CImageControl();
    CleanupStack::PushL(self);
    self->ConstructL(aRect,aParent);
    return self;	
	}

CImageControl::CImageControl()
	{
	iImage = NULL;
	}

CImageControl::~CImageControl()
	{
	delete iImage;
	iImage = NULL;
	}

void CImageControl::ConstructL(const TRect& aRect,const CCoeControl* aParent)
	{
    if(aParent == NULL)
        {
        CreateWindowL();
        }
    else
        {
        SetContainerWindowL(*aParent);
        }
    SetRect(aRect);
    ActivateL();	
	}

void CImageControl::SetLayout()
	{
    TInt ImageHeight= 0;
	if (iImage)
	{
	    iImageRect.SetSize(iImage->SizeInPixels());
	    ImageHeight = iImage->SizeInPixels().iHeight;
	    ImageHeight += 10;
	}
	TRect rect = Rect();
	rect.SetHeight(ImageHeight);
	SetRect(rect);
	}

TInt CImageControl::ImageHeight()
	{
	if (iImage)
		return iImage->SizeInPixels().iHeight;
	return 0;
	}

TInt CImageControl::ImageWidth()
	{
	if (iImage)
		return iImage->SizeInPixels().iWidth;
	return 0;
	}

void CImageControl::SetImage(CImage* aImage)
	{
	if (iImage)
		{
		delete iImage;
		iImage = NULL;
		}
	iImage = aImage;
	}

void CImageControl::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	//gc.SetClippingRect(aRect);
    if (iImage && iImage->Bitmap())
        {
        TPoint position = Position();
        TRect bmpPieceRect(TPoint(0, 0), iImage->SizeInPixels());

        TRect rect = iImageRect;
        TInt xPos = (Rect().Width() - rect.Width()) / 2;
        TInt yPos = (Rect().Height() - rect.Height()) / 2 - 1;
        
        if (iImage->MaskBitmap())
            gc.BitBltMasked(TPoint(position.iX + xPos, position.iY + yPos),iImage->Bitmap(),bmpPieceRect,iImage->MaskBitmap(),EFalse);
        else
            gc.BitBlt(TPoint(position.iX + xPos, position.iY + yPos), iImage->Bitmap(), bmpPieceRect);
        }
	//gc.CancelClippingRect();
	}

TBool CImageControl::IsHasImage() const
{
    if(iImage)
        return ETrue;
    return EFalse;
}
        
